﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class PhieuTraHangDAO
    {
        public string KetNoi()
        {
            string kq = "";
            SqlConnection conn = DataProvider.ConnectDB();
            if (conn == null)
                kq = "THat bai";
            else
                kq = "Thanh cong";
            conn.Close();
            return kq;

        }

        public string ThemPhieuTraHang(PhieuTraHangDTO pthDTO)
        {
            string mess = "";

            string sql = "Insert into";
            sql += " PHIEUTRAHANG(MaPhieuTra, MaNhaCungCap, NgayTra, TongTien)";
            sql += " values(@MaPhieuTra, @MaNhaCungCap, @NgayTra, @TongTien)";
            try
            {
                SqlConnection conn = DataProvider.ConnectDB();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter para;
                para = cmd.Parameters.Add("@MaPhieuTra", SqlDbType.Int);
                para.Value = pthDTO.MaPhieuTra;
                para = cmd.Parameters.Add("@MaNhaCungCap", SqlDbType.Int);
                para.Value = pthDTO.MaNhaCungCap;
                para = cmd.Parameters.Add("@NgayTra", SqlDbType.DateTime);
                para.Value = pthDTO.NgayTra;
                para = cmd.Parameters.Add("@TongTien", SqlDbType.Float);
                para.Value = pthDTO.TongTien;

                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                mess = ex.Message;
            }
            return mess;
        }

        public List<PhieuTraHangDTO> LayDanhSachPTH()
        {
            List<PhieuTraHangDTO> ds = new List<PhieuTraHangDTO>();
            string sql = "Select * From PHIEUTRAHANG";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                PhieuTraHangDTO pthDTO = new PhieuTraHangDTO();
                pthDTO.MaPhieuTra = reader.GetInt32(0);
                pthDTO.MaNhaCungCap = reader.GetInt32(1);
                pthDTO.NgayTra = reader.GetDateTime(2);
                pthDTO.TongTien = reader.GetDouble(3);

                ds.Add(pthDTO);
            }
            conn.Close();
            return ds;
        }

        public int LayMaPhieuTraHang()
        {
            int max = 0;
            List<PhieuTraHangDTO> listPTH = LayDanhSachPTH();
            if (listPTH.Count < 1)
                return max;
            for (int i = 0; i < listPTH.Count; i++)
            {
                if (listPTH[i].MaPhieuTra > max)
                    max = listPTH[i].MaPhieuTra;
            }
            return max;
        }
    }
}
